import "dart:io";

import "package:flutter/material.dart";
import "package:shared_preferences/shared_preferences.dart";

import "../../widgets/appInfo/AppInfoFooter.dart";
import "../PadView.dart";
import "../PhoneView.dart";
import "../WindowsView.dart";

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  Future<void> _logout() async {
    // 添加二次确认对话框
    bool confirm = await showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text("退出登录"),
        content: const Text("您确定要退出登录吗？登出后将清除您在本地的所有未提交的数据！您的部分进度可能会丢失！"),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: const Text("取消"),
          ),
          TextButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: const Text("确认"),
          ),
        ],
      ),
    );

    // 如果用户确认登出，则执行登出操作
    if (confirm == true) {
      final prefs = await SharedPreferences.getInstance();
      prefs.clear();  // 注销时清空本地数据！
      _toWelcomePage();
    }
  }

  Future<void> _toWelcomePage() async {
    final prefs = await SharedPreferences.getInstance();
    final isDarkMode = prefs.getBool('isDarkMode') ?? false;

    if(Platform.isAndroid){
      final bool isMobileLayout = MediaQuery.of(context).size.width < 600;
      if(isMobileLayout){
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => PhoneView(isDarkMode: isDarkMode)));
      }else{
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => PadView(isDarkMode: isDarkMode)));
      }
    }else{   // 目前只支持安卓、Windows
      Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => WindowsView(isDarkMode: isDarkMode)));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('设置'),
      ),
      body: const _SettingPageBody(),
      bottomNavigationBar: Padding(
        padding: const EdgeInsets.all(16.0),
        child: ElevatedButton(
          onPressed: _logout,
          style: ElevatedButton.styleFrom(
            minimumSize: const Size(double.infinity, 50), // 设置按钮宽度为屏幕宽度，高度为50
            backgroundColor: Theme.of(context).colorScheme.error,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10),
            ),
            elevation: 5,
            shadowColor: Colors.grey[400],
            surfaceTintColor: Colors.transparent,
            splashFactory: InkRipple.splashFactory,

          ),
          child: const Text(
            '退出登录',
            style: TextStyle(
              color: Colors.white,
              fontSize: 18,
            ),
          ),
        ),
      ),
    );
  }
}

class _SettingPageBody extends StatelessWidget {
  const _SettingPageBody({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Expanded(
            child: ListView(
              children: [
                _buildMenuItem(Icons.person, "个人资料"),
                _buildMenuItem(Icons.security, "账户安全"),
                _buildMenuItem(Icons.notifications, "通知设置"),
                _buildMenuItem(Icons.settings, "通用设置"),
                _buildMenuItem(Icons.language, "语言设置"),
                _buildMenuItem(Icons.help, "帮助与反馈"),
              ],
            )
        ),
        const AppInfoFooter()
      ],
    );
  }

  Widget _buildMenuItem(IconData icon, String title) {
    return ListTile(
      leading: Icon(icon),
      title: Text(title),
      trailing: const Icon(Icons.arrow_forward_ios),
    );
  }
}